package com.atguigu.serviceedu.api;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.commonutils.vo.CourseWebVoForPay;
import com.atguigu.serviceedu.client.OrderClient;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.vo.ChapterVo;
import com.atguigu.serviceedu.entity.vo.CourseQueryVo;
import com.atguigu.serviceedu.entity.vo.CourseWebVo;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import com.atguigu.commonutils.vo.CourseWebVoForPay;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther 赵乾坤
 * @Date 2020-12-02 15:22
 */
@Api(description = "课程前台展示")
@RestController
@RequestMapping("/eduservice/courseapi")
@CrossOrigin
public class CourseApiController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;
    
    @Autowired
    private OrderClient orderClient;

    //带条件分页查询讲师列表
//    协议描述           @ApiOperation                        用在controller方法上
    @ApiOperation(value = "带条件分页查询课程列表")
    @PostMapping("getCourseApiPageVo/{current}/{limit}")//有改动
//   @GetMapping("getCourseApiPageVo/{current}/{limit}")
    public R getCourseApiPageVo(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody CourseQueryVo courseQueryVo
    ) {
        Page<EduCourse> page = new Page<>(current, limit);
        Map<String,Object> map =  courseService.getCourseApiPageVo(page,courseQueryVo);
        return R.ok().data(map);

    }


    @ApiOperation(value = "前台查询课程详情")
    @GetMapping("getCourseApiInfo/{courseId}")//多一个参数看课程是否被购买过
    public R getCourseApiInfo(@PathVariable String courseId,
                              HttpServletRequest request){
        //1课程信息+课程描述+讲师信息
        CourseWebVo courseWebVo = courseService.getCourseApiInfo(courseId);
        //2 课程大纲数据
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoById(courseId);
        //3课程是否被该用户购买
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("memberId="+memberId);
        boolean isPayCourse  = orderClient.isPayCourse(courseId, memberId);

        return R.ok().data("courseWebVo",courseWebVo)
                .data("chapterVoList",chapterVoList)
                .data("isPayCourse ",isPayCourse);
    }

    //不能依赖 等会老师
    @ApiOperation(value = "查询课程信息，远程调用")
    @GetMapping("getCourseInfoForOrder/{courseId}")
    public CourseWebVoForPay getCourseInfoForOrder(@PathVariable("courseId")String courseId){
        CourseWebVo  courseWebVo  = courseService.getCourseApiInfo(courseId);
        CourseWebVoForPay courseWebVoForPay = new CourseWebVoForPay();
        BeanUtils.copyProperties(courseWebVo,courseWebVoForPay);
        return courseWebVoForPay;
    }



}
